# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

import time
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class PointLogDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()


    def selectPointLog(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_point_log WHERE id=%s "

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr


    def add_point_log(self, col_info):

        user_id = col_info['user_id']
        username = col_info['username']
        point_item_name = col_info['point_item_name']
        point_value = col_info['point_value']
        remark = col_info['remark']
        is_income = col_info['is_income']
        create_time = col_info['create_time']
        sql = "insert into zzh_point_log (user_id,username,point_item_name,point_value,remark,is_income,create_time) VALUES (%s,%s,%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [user_id,username,point_item_name,point_value,remark,is_income,create_time])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def del_point_log(self, ids):
        cur = self.conn.cursor()
        sql = "delete from zzh_point_log WHERE id=%s"
        for log_id in ids:
            cur.execute(sql, [log_id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return 1

    def query_point_log(self, log_id):

        sql = "select * from zzh_point_log WHERE id=%s "
        cur = self.conn.cursor()
        total_count = cur.execute(sql, [log_id])
        if total_count:
            point_item_list = cur.fetchall()
            cur.close()
            self.conn.close()
            point_item_json = Table2JsonByYM.point_log_listto_json(point_item_list)
            return total_count, point_item_json
        else:
            return 0, None




    def queryPointLogByCondition(self,**kwargs):


        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']




        fieldMap = requestFieldMapToDBField_YM.pointLogMap#要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_point_log WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_point_log WHERE " + whereSentence
        rs = cur.execute(sql)

        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            resultList= MyPaging().getPage(total_count,pageIndex,pageSize,result)
            userOauthJSON =Table2JsonByYM.point_log_listto_json(resultList)
            return total_count,userOauthJSON
        else:

            cur.close()
            self.conn.close()
            return 0,None

    def update_point_log(self, new_point_log, point_log_id):
        user_id = new_point_log['user_id']
        username = new_point_log['username']
        point_item_name = new_point_log['point_item_name']
        point_value = new_point_log['point_value']
        remark = new_point_log['remark']
        is_income = new_point_log['is_income']
        create_time = new_point_log['create_time']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_point_log SET user_id=%s,username=%s,point_item_name=%s,point_value=%s ,remark=%s,is_income=%s ,create_time=%s WHERE id=%s "
        update_point_log = cur.execute(sql, [user_id,username,point_item_name,point_value,remark,is_income,create_time,point_log_id])
        self.conn.commit()
        if update_point_log:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0
